package com.lagou.edu.service.impl;

import com.lagou.edu.bean.LagouToken;
import com.lagou.edu.bean.User;
import com.lagou.edu.common.BusinessException;
import com.lagou.edu.dao.LagouTokenDao;
import com.lagou.edu.dao.UserDao;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import service.CodeService;
import service.UserService;

import java.util.Date;
import java.util.Optional;
import java.util.UUID;

/**
 * @ClassName UserServiceImpl
 * @Description TODO
 * @Auth ying.xiao
 * @Date 2021/4/20
 * @Version 1.0.0
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private LagouTokenDao lagouTokenDao;

    @Reference
    private CodeService codeService;


    public String register(String email, String password, String code) {

        //验证注册信息
        Integer validate = codeService.validate(email, code);
        if(validate == 0){

            //保存登录注册信息
            User user = new User();
            user.setEmail(email);
            Optional<User> one = userDao.findOne(Example.of(user));
            if(one.isPresent()){
                throw  new BusinessException("99","该账号已经注册，无需反复注册");
            }
            user.setPassword(password);
            user.setCreatetime(new Date());
            userDao.save(user);

            //验证通过保存token
            LagouToken lagouToken = new LagouToken();
            lagouToken.setEmail(email);
            lagouToken.setToken(UUID.randomUUID().toString());
            lagouTokenDao.save(lagouToken);
            return lagouToken.getToken();
        }
        if(validate == 1){
            throw  new BusinessException("99","验证码错误");
        }
        if(validate == 2){
            throw  new BusinessException("99","验证码超时，请重新获取");
        }
        return null;

    }


    public boolean isRegister(String email) {
        User user = new User();
        user.setEmail(email);
        User user1 = userDao.findOne(Example.of(user)).get();
        if (null != user1 && null != user1.getId()){
            return true;
        }
        return false;
    }


    public String login(String email, String password) {

        //验证登录信息
        User user = new User();
        user.setEmail(email);
        user.setPassword(password);
        Optional<User> one = userDao.findOne(Example.of(user));
        if(!one.isPresent()){
            throw new BusinessException("99","该用户不存在，请注册用户");
        }
        User user1 = userDao.findOne(Example.of(user)).get();

        //验证通过保存token
        if (null != user1 && null != user1.getId()){
            LagouToken lagouToken = new LagouToken();
            lagouToken.setEmail(email);
            lagouToken.setToken(UUID.randomUUID().toString());
            lagouTokenDao.save(lagouToken);
            return lagouToken.getToken();
        }
        return null;
    }

    @Override
    public String info(String token) {
        LagouToken lagouToken = new LagouToken();
        lagouToken.setToken(token);
        Optional<LagouToken> one = lagouTokenDao.findOne(Example.of(lagouToken));
        if(one.isPresent()){
            LagouToken lagouToken1 = one.get();
            if(null != lagouToken1&&null != lagouToken1.getId()){
                return lagouToken1.getEmail();
            }
        }
        return null;
    }
}
